package gu.sql2java.transformer;

import static com.google.common.base.Preconditions.checkArgument;

import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import com.google.common.collect.ImmutableSet;
/**
 * @author guyadong
 * @since 3.21.0
 */
public class ColumnTransformer extends BaseTypeTransformer {
	/**
	 * @since 3.34.0
	 */
	public static final  ColumnTransformer COLUMN_TRANSFORMER = new ColumnTransformer();
	/**
	 * @since 3.34.0
	 */
	public static final Set<Class<?>> INTERGRAL_CLASSES = ImmutableSet.<Class<?>>of(Long.class,Integer.class,Short.class,Byte.class);
	public ColumnTransformer() {
		super();
	}
	/**
	 * 输入的整型值转为无符号的long,非整型参数返回{@code null}
	 * @param input
	 * @since 3.34.0
	 */
	public static Long asUnsignedLong(Object input) {
		if(input instanceof Long){
			return (Long)input;
		}else if(input instanceof Integer) {
			return ((Integer)input).longValue()&0xffffffff;
		}else if(input instanceof Short) {
			return ((Short)input).longValue()&0xffff;
		}else if(input instanceof Byte) {
			return ((Byte)input).longValue()&0xff;
		}else if(input instanceof AtomicLong){
			return ((AtomicLong)input).longValue();
		}else if(input instanceof AtomicInteger){
			return ((AtomicInteger)input).longValue()&0xffffffff;
		}else  if(input instanceof Number) {
			return ((Number)input).longValue();
		}
		return null;
	}
	/**
	 * 输入的整型值转为无符号的long,非整型参数抛出异常
	 * @param input
	 * @since 3.27.0
	 */
	public static long asUnsignedLongChecked(Object input) {
		Long m = asUnsignedLong(input);
		checkArgument(null!=m,"Integral type required for mask");
		return m;
	}
}
